#include "report.h"

Report::Report(ExpenseRecordList expense, IncomeRecordList income, vector<QString> reportOptionIncomeCategory, vector<QString> reportOptionExpenseCategory,
               string type, string date, double toPrice, double fromPrice){
    for(int i = 0; i < reportOptionIncomeCategory.size(); i++)
        qDebug()<< reportOptionIncomeCategory[i];
    int j = 0;
    for(j = 0; j < reportOptionIncomeCategory.size(); j++)
        qDebug()<< "Income category: " << reportOptionIncomeCategory[j];

    for(j = 0; j < reportOptionExpenseCategory.size(); j++)
        qDebug()<< "Expense category: " << reportOptionExpenseCategory[j];

    qDebug()<<"Type is: " << QString::fromStdString(type);
    qDebug()<<"Date option is: " << QString::fromStdString(date);
    qDebug()<<"Price from: " << fromPrice << " to: " << toPrice;

    setupUi(this);
    reportBalance->setText(QString::number((income.calculateIncome() - expense.calculateExpense())));
    scene = new QGraphicsScene(this);
    graphicsView->setScene( scene);

    QVector<QPointF> points;
    double i = 0.0;

    // now comes the hard part.
    incomerecords.clearList();
    expenserecords.clearList();
    if(type == "All"){
        searchExpense(expense, reportOptionExpenseCategory, date, toPrice, fromPrice);
        searchIncome(income, reportOptionIncomeCategory, date, toPrice, fromPrice);
    }
    else if(type == "Expense"){
        searchExpense(expense, reportOptionExpenseCategory, date, toPrice, fromPrice);
    }
    else if(type == "Income"){
        searchIncome(income, reportOptionIncomeCategory, date, toPrice, fromPrice);
    }

    incomerecords.printList();
    expenserecords.printList();

    double totalIncome = incomerecords.calculateIncome();
    double totalExpense = expenserecords.calculateExpense();

    double incomeAngle = totalIncome/(totalIncome + totalExpense);
    double expenseAngle = totalExpense/(totalIncome + totalExpense);

    incomeAngle *= 360;
    expenseAngle *= 360;

    for(i = 0; i <= incomeAngle; i+=0.1){
        points.push_back(QPointF(cos(i*PI/180),sin(i*PI/180))*80);
    }
    points.push_back(QPointF(0,0));
    QPolygonF incomeSlice = QPolygonF(points);
    points.clear();

    for(i = 0; i <= (360); i+= 0.1){
        points.push_back(QPointF(cos(i*PI/180),sin(i*PI/180))*80);
    }
    QPolygonF expenseSlice = QPolygonF(points);
    points.clear();

    if(incomeAngle > expenseAngle){
        scene->addPolygon(incomeSlice, QPen(Qt::black), QBrush(Qt::green));
        scene->addPolygon(expenseSlice, QPen(Qt::black), QBrush(Qt::red));
    }
    else{
        scene->addPolygon(expenseSlice, QPen(Qt::black), QBrush(Qt::red));
        scene->addPolygon(incomeSlice, QPen(Qt::black), QBrush(Qt::green));
    }
    reportExpenseTotal->setText(QString::number(expenserecords.calculateExpense()));
    reportIncomeTotal->setText(QString::number(incomerecords.calculateIncome()));
    reportBalance->setText(QString::number(incomerecords.calculateIncome() - expenserecords.calculateExpense()));

}

void Report::searchExpense(ExpenseRecordList expense, vector<QString> reportOptionExpenseCategory, string date, double toPrice, double fromPrice){
    int j = 0;
    int i = 0;
    for(j = 0; j < expense.size(); j++){
        if(date == "All Time"){
            if(toPrice != 0){
                if(expense[j].getamount() >= fromPrice && expense[j].getamount() <= toPrice){
                    if(!reportOptionExpenseCategory.empty()){
                        for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                            if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                                expenserecords.addRecord(expense[j]);
                    }
                    else
                        expenserecords.addRecord(expense[j]);
                }
            }
            else{
                if(!reportOptionExpenseCategory.empty()){
                    for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                        if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                            expenserecords.addRecord(expense[j]);
                }
                else
                    expenserecords.addRecord(expense[j]);
            }
        }
        else if(date == "Year"){
            if(expense[j].getdatetime() >= QDateTime(QDate(QDateTime::currentDateTime().date().year() - 1,QDateTime::currentDateTime().date().month(),
                                                           QDateTime::currentDateTime().date().day()), QTime::currentTime())){
                if(toPrice != 0){
                    if(expense[j].getamount() >= fromPrice && expense[j].getamount() <= toPrice){
                        if(!reportOptionExpenseCategory.empty()){
                            for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                                if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                                    expenserecords.addRecord(expense[j]);
                        }
                        else
                            expenserecords.addRecord(expense[j]);
                    }
                }
                else{
                    if(!reportOptionExpenseCategory.empty()){
                        for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                            if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                                expenserecords.addRecord(expense[j]);
                    }
                    else
                        expenserecords.addRecord(expense[j]);
                }
            }
        }
        else if(date == "Month"){
            if(expense[j].getdatetime() >= QDateTime(QDate(QDateTime::currentDateTime().date().year(),QDateTime::currentDateTime().date().month() - 1,
                                                           QDateTime::currentDateTime().date().day()), QTime::currentTime())){
                if(toPrice != 0){
                    if(expense[j].getamount() >= fromPrice && expense[j].getamount() <= toPrice){
                        if(!reportOptionExpenseCategory.empty()){
                            for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                                if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                                    expenserecords.addRecord(expense[j]);
                        }
                        else
                            expenserecords.addRecord(expense[j]);
                    }
                }
                else{
                    if(!reportOptionExpenseCategory.empty()){
                        for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                            if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                                expenserecords.addRecord(expense[j]);
                    }
                    else
                        expenserecords.addRecord(expense[j]);
                }
            }
        }
        else if(date == "Week"){
            if(expense[j].getdatetime() >= QDateTime(QDate(QDateTime::currentDateTime().date().year(),QDateTime::currentDateTime().date().month(),
                                                           QDateTime::currentDateTime().date().day() - 7), QTime::currentTime())){
                if(toPrice != 0){
                    if(expense[j].getamount() >= fromPrice && expense[j].getamount() <= toPrice){
                        if(!reportOptionExpenseCategory.empty()){
                            for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                                if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                                    expenserecords.addRecord(expense[j]);
                        }
                        else
                            expenserecords.addRecord(expense[j]);
                    }
                }
                else{
                    if(!reportOptionExpenseCategory.empty()){
                        for(i = 0; i < reportOptionExpenseCategory.size(); i++)
                            if(QString::fromStdString(expense[j].getcategory()) == reportOptionExpenseCategory[i])
                                expenserecords.addRecord(expense[j]);
                    }
                    else
                        expenserecords.addRecord(expense[j]);
                }
            }
        }
    }
}
void Report::searchIncome(IncomeRecordList income, vector<QString> reportOptionIncomeCategory, string date, double toPrice, double fromPrice){
    int j = 0;
    int i = 0;
    for(j = 0; j < income.size(); j++){
        if(date == "All Time"){
            if(toPrice != 0){
                if(income[j].getamount() >= fromPrice && income[j].getamount() <= toPrice){
                    if(!reportOptionIncomeCategory.empty()){
                        for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                            if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                                incomerecords.addRecord(income[j]);
                    }
                    else
                        incomerecords.addRecord(income[j]);
                }
            }
            else{
                if(!reportOptionIncomeCategory.empty()){
                    for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                        if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                            incomerecords.addRecord(income[j]);
                }
                else
                    incomerecords.addRecord(income[j]);
            }
        }
        else if(date == "Year"){
            if(income[j].getdatetime() >= QDateTime(QDate(QDateTime::currentDateTime().date().year() - 1,QDateTime::currentDateTime().date().month(),
                                                          QDateTime::currentDateTime().date().day()), QTime::currentTime())){
                if(toPrice != 0){
                    if(income[j].getamount() >= fromPrice && income[j].getamount() <= toPrice){
                        if(!reportOptionIncomeCategory.empty()){
                            for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                                if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                                    incomerecords.addRecord(income[j]);
                        }
                        else
                            incomerecords.addRecord(income[j]);
                    }
                }
                else{
                    if(!reportOptionIncomeCategory.empty()){
                        for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                            if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                                incomerecords.addRecord(income[j]);
                    }
                    else
                        incomerecords.addRecord(income[j]);
                }

            }
        }
        else if(date == "Month"){
            if(income[j].getdatetime() >= QDateTime(QDate(QDateTime::currentDateTime().date().year(),QDateTime::currentDateTime().date().month() - 1,
                                                          QDateTime::currentDateTime().date().day()), QTime::currentTime())){
                if(toPrice != 0){
                    if(income[j].getamount() >= fromPrice && income[j].getamount() <= toPrice){
                        if(!reportOptionIncomeCategory.empty()){
                            for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                                if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                                    incomerecords.addRecord(income[j]);
                        }
                        else
                            incomerecords.addRecord(income[j]);
                    }
                }
                else{
                    if(!reportOptionIncomeCategory.empty()){
                        for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                            if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                                incomerecords.addRecord(income[j]);
                    }
                    else
                        incomerecords.addRecord(income[j]);
                }

            }
        }
        else if(date == "Week"){
            if(income[j].getdatetime() >= QDateTime(QDate(QDateTime::currentDateTime().date().year(),QDateTime::currentDateTime().date().month(),
                                                          QDateTime::currentDateTime().date().day() - 7), QTime::currentTime())){
                if(toPrice != 0){
                    if(income[j].getamount() >= fromPrice && income[j].getamount() <= toPrice){
                        if(!reportOptionIncomeCategory.empty()){
                            for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                                if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                                    incomerecords.addRecord(income[j]);
                        }
                        else
                            incomerecords.addRecord(income[j]);
                    }
                }
                else{
                    if(!reportOptionIncomeCategory.empty()){
                        for(i = 0; i < reportOptionIncomeCategory.size(); i++)
                            if(QString::fromStdString(income[j].getcategory()) == reportOptionIncomeCategory[i])
                                incomerecords.addRecord(income[j]);
                    }
                    else
                        incomerecords.addRecord(income[j]);
                }
            }
        }
    }
}
